package com.ubs.training.server.service;

import com.ubs.training.prod.server.service.LoggingService;
import com.ubs.training.server.dao.ContactDao;
import com.ubs.training.server.dao.impl.hbm.HbmContactDao;
import com.ubs.training.server.dao.model.Contact;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xubuntu
 * Date: 8/22/12
 * Time: 3:59 AM
 * To change this template use File | Settings | File Templates.
 */
@Service
public class ContactService {
    public static final String USER_REQUESTS_ALL_CONTACTS = "Stupid user requests all contacts";
    public static final String USER_REQUESTS_CONTACT_ADD = "Stupid user requests contact save-adding";
    public static final String USER_REQUESTS_CONTACT_DELETE = "Stupid user requests contact deletion";
    public static final String USER_REQUESTS_CONTACT_UPDATE = "Stupid user requests contact save-updating";
    public static final String USER_REQUESTS_ALL_CONTACTS_OF_CLIENT = "Stupid user requests all contacts of a client";
    public static final String USER_REQUESTS_A_CONTACT_BY_ID = "Stupid user requests a contact by ID";
    public static final String USER_REQUESTS_CONTACT_SAVE = "Stupid user requests save contact";

    @Resource(name = "hbmContactDao")
   // @Resource(name = "contactDao")
    ContactDao dao;

    @Autowired
    private LoggingService svc;


    public LoggingService getSvc() {
        return svc;
    }

    public void setSvc(LoggingService svc) {
        this.svc = svc;
    }

    public ContactDao getDao() {
        return dao;
    }

    public void setDao(ContactDao dao) {
        this.dao = dao;
    }

    public List<Contact> getAllContacts() {
      //  svc.logEvent(USER_REQUESTS_ALL_CONTACTS);
        return dao.getAll();
    }


    public List<Contact> getAllContactsByClientID(int id)
    {
        //svc.logEvent(USER_REQUESTS_CONTACT_BY_CLIENT_ID);
        return dao.getAllContactsOfClient(id);
    }


    public Contact getContactByID(int id){
        return dao.getContactByID(id);
    }

    public void save(Contact contact){

        if (contact.getContactID() == 0) {
            dao.add(contact);
    //        svc.logEvent(USER_REQUESTS_CONTACT_ADD);
        }
        else
        {
            dao.update(contact);
      //      svc.logEvent(USER_REQUESTS_CONTACT_UPDATE);
        }


    }

    public void deleteContact(Contact contact){
      //  svc.logEvent(USER_REQUESTS_CONTACT_DELETE);
        List<Contact> allContacts=dao.getAll();
        if (allContacts.contains(contact))
        {
            dao.removeContact(contact);
        }
    }

    public void deleteContact(int id){
  //      svc.logEvent(USER_REQUESTS_CONTACT_DELETE);
          dao.removeContact(id);
    }
}
